/*
 * Copyright (c) 2023 Cshoo Org. All Rights Reserved.
 */

package org.cshoo.tattoo.authorize.domain.repository;

import org.cshoo.tattoo.authorize.domain.po.Role;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;

import java.util.List;

/**
 * @author 杨翼
 * @since 1.0
 */
public interface IRoleRepository extends CrudRepository<Role, Long> {

    Page<Role> findAll(Pageable pageable);

    @Query(value = "select role from Role role inner join GroupRoleRelation grr on role.id = grr.roleId inner join GroupUserRelation gur on grr.groupId = gur.groupId where gur.userId = ?1")
    List<Role> findRolesByUserId(Long userId);

    @Query(value = "SELECT role FROM Role role INNER JOIN GroupRoleRelation groupRoleRelation ON role.id = groupRoleRelation.roleId WHERE groupRoleRelation.groupId = ?1")
    List<Role> findRolesByGroupId(Long groupId);

    @Query(value = "SELECT r FROM Role r WHERE r.name LIKE CONCAT('%',?1,'%')")
    List<Role> findRolesByName(String name, Pageable pageable);

    Role findRoleByName(String name);
}
